﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RFIDStockManagement.Common.Public.InterFace;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Common.Public.Enums;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.Client.Report
{
    public partial class StockCheckFilter  : DevExpress.XtraEditors.XtraForm, IReportFilter
    {
        private StockCheckManager _thisManager;
        private bool isGetOk;
        private Dictionary<string, object> filters;

        public StockCheckFilter()
        {
            InitializeComponent();
            filters = new Dictionary<string, object>();
            _thisManager = new StockCheckManager();
        }

        #region IReportFilter 成员

        public Dictionary<string, object> Filters
        {
            get
            {
                return filters;
            }
            set
            {
               filters = value;
            }
        }

        public bool IsGetOk
        {
            get { return isGetOk; }
        }

        public bool Show(string parms)
        {
            int checkId;
            object tryOjb;

            isGetOk = false;

            if (filters.TryGetValue("stockcheckId", out tryOjb))
            {
                InitCheckInfos((int)tryOjb);
            }
            else
            {
                InitCheckInfos(0);
            }

            if (filters.TryGetValue("stockId", out tryOjb))
            {
                InitStockList((long)tryOjb);
            }
            else
            {
                InitStockList(0);
            }

            if (filters.TryGetValue("FItemStart", out tryOjb))
            {
                txtItemStart.Text = tryOjb.ToString();
            }
            else
            {
                txtItemStart.Text = string.Empty ;
            }

            if (filters.TryGetValue("FItemEnd", out tryOjb))
            {
                txtItemEnd.Text = tryOjb.ToString();
            }
            else
            {
                txtItemEnd.Text = string.Empty;
            }


            this.ShowDialog();

            return isGetOk;
        }


        #endregion

        private void bntOk_Click(object sender, EventArgs e)
        {
            ComBoxItem selItem;
            isGetOk = true;
            selItem = combList.SelectedItem as ComBoxItem;

            filters.Clear();
            if (selItem != null)
            {
                filters.Add("stockcheckId", selItem.Item);
            }

            selItem = combStockList.SelectedItem as ComBoxItem;
            if (selItem != null)
            {
                filters.Add("stockId", selItem.Item);
            }
            filters.Add("FItemStart", txtItemStart.Text.Trim());
            filters.Add("FItemEnd", txtItemEnd.Text.Trim());

            this.Hide();

        }

        private void bntCancel_Click(object sender, EventArgs e)
        {
            isGetOk = false;
            this.Hide();
        }

        private void InitCheckInfos(int id)
        {

            combList.Properties.Items.Clear();
            List<StockCheckInfo> checks = _thisManager.GetList(EnumStockCheckStatus.Default);
            int index;

            combList.Properties.BeginUpdate();
            foreach (StockCheckInfo x in checks)
            {
                index = combList.Properties.Items.Add(new ComBoxItem(x.FCaption, x.FId));
                if (x.FId == id)
                {
                    combList.SelectedIndex = index;
                }
            }

            combList.Properties.EndUpdate();
        }

        private void InitStockList(long id)
        {
            int index;
            DataTable dtStockList = BasicInfoManager.GetList(null, EnumBasicInfo.Stock);

            combStockList.Properties.BeginUpdate();

            combStockList.Properties.Items.Add(new ComBoxItem("全部", 0));

            foreach (DataRow xx in dtStockList.Rows)
            {
                index= combStockList.Properties.Items.Add(new ComBoxItem(xx["FName"].ToString(), PublicFunction.NotNullLng(xx["Fid"])));
                if (PublicFunction.NotNullLng(xx["Fid"]) == id)
                {
                    combStockList.SelectedIndex = index;
                }
            }
            combStockList.Properties.EndUpdate();
        }


    }
}
